using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using MainGame.Menu;
using Services;
using MainGame.GamePlay;
using MainGame.GameSound;
using MainGame.SaveData;
using Microsoft.Phone.Shell;
using MainGame.Utilities;

#if WINDOWS_PHONE
  using Advertisement;
#endif


namespace MainGame.StateManagement
{
  /// <summary>
  /// Una lista che mantiene i punteggi di tutti i giocatori per ogni round di gioco
  /// </summary>
  public struct PartialScores
  {
    public static List<Score> scoreList;
  }

  /// <summary>
  /// Funzioni statiche (invocabili da una qualsiasi istanza di Game) che si
  /// occupano di attivare i componenti che caratterizzano ciascuna fase di gioco
  /// </summary>
  public static class StateManager
  {
    // Menus
    private static MainMenu mainMenu;
    private static SettingsMenu settingsMenu;
    private static IntroMenu intro;
    private static StartMenu startMenu;
    public static CreditsMenu creditsMenu;
    public static HelpMenu helpMenu;
    public static MenuPause menuPause;
    public static BackgroundMenu bgMenu;
    public static KeyboardMenu keyboardMenu;
    public static ProfileMenu profileMenu;
    public static GameSettings gameSettings;
    public static GraphicsSettings graphicsSettings;
    public static AudioSettings audioSettings;
    //public static ChartMenu HighScoresChart;
    public static DecorationPanel decorationPanel;
    
    /// <summary>
    /// Avviamo il menu
    /// </summary>
    /// <param name="game"></param>
    static public void StartMenu(this Game game)
    {
      game.Cleanup();
      // Menu and game components
      game.Components.Add(new InputManager(game));      
      startMenu = new StartMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(startMenu);
      decorationPanel = new DecorationPanel(game, MenuDepths.Decoration);
      game.Components.Add(decorationPanel);
      mainMenu = new MainMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(mainMenu);
      settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
      game.Components.Add(settingsMenu);
      creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
      game.Components.Add(creditsMenu);
      bgMenu = new BackgroundMenu(game, MenuDepths.Background);
      game.Components.Add(bgMenu);
      game.Components.Add(new MenuSoundManager(game));
      var menu = game.Services.GetService(typeof(IMainMenu)) as IMainMenu;
      var startmenu = game.Services.GetService(typeof(IStartMenu)) as IStartMenu;
      creditsMenu.Enabled = false;
      settingsMenu.Enabled = false;
      decorationPanel.direction = 0;
      // Events
      var context = game.GetContext();
      startmenu.OnPlayClicked += () => { game.GetContext().currentGame = null; game.StartLevel(1); };
      startmenu.OnExitClicked += () =>
      {
        // Save the default information
        DefaultInfo info = game.LoadDefaults();
        info.lastUser = context.currentProfile.id;
        //info.lastProfileId = game.GetProfileList().Count;
        game.SaveDefaults(info);
        // Save the configuration for the current profile
        game.SaveConfig(context.currentConfig, context.currentProfile.id);
        // Save the high-scores chart
        //game.SaveChart(game.GetContext().currentChart);
#if WINDOWS_PHONE
        game.ShowAds();
#else
        game.Exit();
#endif
      };
      startmenu.OnContinue += () => 
      {
        context.currentGame = game.LoadGame(context.currentProfile.id);
        game.StartLevel(1);
        ui_renderer.elapsedTime = context.currentGame.timeElapsed;
      };
      menu.OnSettingsClicked += () => game.ShowSettings();
      menu.OnCreditsClicked += () => game.ShowCredits();
      menu.OnHelpClicked += () => game.ShowHelp();
      //menu.OnHighScoresClicked += () => game.ShowHighScores();
      startmenu.OnProfile += () => game.ShowProfile();
      startmenu.OnNewProfile += () => game.ShowKeyboard(false);
    }

    /// <summary>
    /// Animazione Introduttiva
    /// </summary>
    /// <param name="game"></param>
    static public void Intro(this Game game)
    {
      game.Cleanup();

      game.Components.Add(new InputManager(game));
      game.Components.Add(new MenuSoundManager(game));
      intro = new IntroMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(intro);

      intro.Animated = true;
      intro.OnStartGame += () => { game.StartMenu(); };
      //intro.OnExit += () => { game.ShowAds(); };
    }

    /// <summary>
    /// Mostra la schermata delle impostazioni
    /// </summary>
    /// <param name="game"></param>
    static public void ShowSettings(this Game game)
    {
      first_transition = true;
      game.Cleanup();
      game.Components.Add(new InputManager(game));
      mainMenu = new MainMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(mainMenu);
      decorationPanel = new DecorationPanel(game, MenuDepths.Decoration);
      game.Components.Add(decorationPanel);
      gameSettings = new GameSettings(game, MenuDepths.ForeGround);
      game.Components.Add(gameSettings);
      gameSettings.Enabled = false;
      gameSettings.startSettings = true;
      decorationPanel.direction = 0;

      graphicsSettings = new GraphicsSettings(game, MenuDepths.ForeGround);
      game.Components.Add(graphicsSettings);
      graphicsSettings.Enabled = false;
      graphicsSettings.Visible = false;

      audioSettings = new AudioSettings(game, MenuDepths.ForeGround);
      game.Components.Add(audioSettings);
      audioSettings.Enabled = false;
      audioSettings.Visible = false;

      settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
      game.Components.Add(settingsMenu);
      creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
      game.Components.Add(creditsMenu);
      bgMenu = new BackgroundMenu(game, MenuDepths.Background);
      game.Components.Add(bgMenu);
      mainMenu.Enabled = false;
      settingsMenu.Animated = true;
      creditsMenu.Enabled = false;
      
      game.Components.Add(new MenuSoundManager(game));      
      var menu = game.Services.GetService(typeof(ISettingsMenu)) as ISettingsMenu;
      menu.OnSBackClicked += () =>
      {
        first_transition = true;
        game.SaveConfig(game.GetContext().currentConfig, game.GetContext().currentProfile.id);
        game.StartMenu();
      };
      menu.OnGameSettings += () => { first_transition = true; game.ShowGameSettings(); };
      menu.OnGraphicsSettings += () => { first_transition = true; game.ShowGraphicsSettings(); };
      menu.OnAudioSettings += () => { first_transition = true; game.ShowAudioSettings(); };
      //menu.OnWIN += () => { game.LevelRecapScreen(0, new Score { PlayerScores = new List<PlayerScore>() }); };
    }

    static public void ShowGameSettings(this Game game)
    {
      first_transition = true;
      
      graphicsSettings.Enabled = false;
      graphicsSettings.Visible = false;
      gameSettings.Enabled = true;
      gameSettings.Visible = true;
      audioSettings.Enabled = false;
      audioSettings.Visible = false;     
    }

    static public void ShowGraphicsSettings(this Game game)
    {
      first_transition = true;
      graphicsSettings.Enabled = true;
      graphicsSettings.Visible = true;
      gameSettings.Enabled = false;
      gameSettings.Visible = false;
      audioSettings.Enabled = false;
      audioSettings.Visible = false;
      }

    static public void ShowAudioSettings(this Game game)
    {
      first_transition = true;
      graphicsSettings.Enabled = false;
      graphicsSettings.Visible = false;
      gameSettings.Enabled = false;
      gameSettings.Visible = false;
      audioSettings.Enabled = true;
      audioSettings.Visible = true;
    }


    /// <summary>
    /// Mostra la schermata dei credits
    /// </summary>
    /// <param name="game"></param>
    static public void ShowCredits(this Game game)
    {
      first_transition = true;
      game.Cleanup();
      game.Components.Add(new InputManager(game));
      mainMenu = new MainMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(mainMenu);
      settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
      game.Components.Add(settingsMenu);
      creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
      game.Components.Add(creditsMenu);
      bgMenu = new BackgroundMenu(game, MenuDepths.Background);
      game.Components.Add(bgMenu);

      //To hide back button from SettingsMenu
      settingsMenu.buttons.Remove(settingsMenu.buttons.ElementAt(0));

      creditsMenu.Animated = true;      
      mainMenu.Enabled = false;
      settingsMenu.Enabled = false;      
      game.Components.Add(new MenuSoundManager(game));
      var menu = game.Services.GetService(typeof(ICreditsMenu)) as ICreditsMenu;
      menu.OnCBackClicked += () => { first_transition = true; game.StartMenu(); };

    }

    /// <summary>
    /// Mostra la schermata di istruzioni
    /// </summary>
    /// <param name="game"></param>
    static public void ShowHelp(this Game game)
    {
      first_transition = true;
      game.Cleanup();
      game.Components.Add(new InputManager(game));
      helpMenu = new HelpMenu(game, MenuDepths.ForeGround);
      game.Components.Add(helpMenu);
      startMenu = new StartMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(startMenu);
      mainMenu = new MainMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(mainMenu);
      settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
      game.Components.Add(settingsMenu);
      creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
      game.Components.Add(creditsMenu);
      bgMenu = new BackgroundMenu(game, MenuDepths.Background);
      game.Components.Add(bgMenu);      
      helpMenu.Animated = true;
      mainMenu.Enabled = false;
      creditsMenu.Enabled = false;      
      settingsMenu.Enabled = false;
      mainMenu.Enabled = false;
      game.Components.Add(new MenuSoundManager(game));
      var menu = game.Services.GetService(typeof(IHelpMenu)) as IHelpMenu;
      menu.OnHBackClicked += () => { first_transition = true; game.StartMenu(); };

    }

    /// <summary>
    /// Mostra la schermata degli high scores
    /// </summary>
    /// <param name="game"></param>
    //static public void ShowHighScores(this Game game)
    //{
    //  first_transition = true;
    //  game.Cleanup();
    //  game.Components.Add(new InputManager(game));
    //  mainMenu = new MainMenu(game, MenuDepths.FrontMenu);
    //  game.Components.Add(mainMenu);
    //  settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
    //  game.Components.Add(settingsMenu);
    //  creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
    //  game.Components.Add(creditsMenu);
    //  bgMenu = new BackgroundMenu(game, MenuDepths.Background);
    //  game.Components.Add(bgMenu);
    //  mainMenu.Enabled = false;
    //  settingsMenu.Enabled = false;
    //  creditsMenu.Enabled = false;
    //  bgMenu.Enabled = false;

    //  HighScoresChart = new ChartMenu(game, MenuDepths.ForeGround);
    //  game.Components.Add(HighScoresChart);

    //  //To hide back button from SettingsMenu
    //  //settingsMenu.buttons.Remove(settingsMenu.buttons.ElementAt(0));

    //  mainMenu.Enabled = false;
    //  //settingsMenu.Enabled = false;
    //  game.Components.Add(new MenuSoundManager(game));
    //  var menu = game.Services.GetService(typeof(IChart)) as IChart;
    //  menu.OnCBackClicked += () => { first_transition = true; game.StartMenu(); };
    //  HighScoresChart.Animated = true;

    //}


    /// <summary>
    /// Mostra la tastiera
    /// </summary>
    /// <param name="game"></param>
    /// 
    static public void ShowKeyboard(this Game game, bool fromProfile)
    {
      game.Cleanup();
      game.Components.Add(new InputManager(game));
      keyboardMenu = new KeyboardMenu(game, MenuDepths.ForeGround);
      game.Components.Add(keyboardMenu);
      startMenu = new StartMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(startMenu);
      mainMenu = new MainMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(mainMenu);
      settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
      game.Components.Add(settingsMenu);
      creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
      game.Components.Add(creditsMenu);
      bgMenu = new BackgroundMenu(game, MenuDepths.Background);
      game.Components.Add(bgMenu);
      keyboardMenu.fromProfile = fromProfile;
      
      
      keyboardMenu.Animated = true;
      mainMenu.Enabled = false;
      creditsMenu.Enabled = false;
      settingsMenu.Enabled = false;
      mainMenu.Enabled = false;
      startMenu.Enabled = false;
      
      game.Components.Add(new MenuSoundManager(game));
      var menu = game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
      menu.OnSaveClicked += () => { first_transition = true; game.StartMenu(); };
      menu.OnExitClicked += () => { first_transition = true; game.ShowProfile(); };
      menu.OnExitClickedNoProfile += () => { first_transition = true; game.StartMenu(); };

    }

    /// <summary>
    /// Mostra la schermata di inserimento di un nuovo profilo
    /// </summary>
    /// <param name="game"></param>
    static public void ShowProfile(this Game game)
    {
      game.Cleanup();
      game.Components.Add(new InputManager(game));
      profileMenu = new ProfileMenu(game, MenuDepths.ForeGround);
      game.Components.Add(profileMenu);
      startMenu = new StartMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(startMenu);
      mainMenu = new MainMenu(game, MenuDepths.FrontMenu);
      game.Components.Add(mainMenu);
      settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
      game.Components.Add(settingsMenu);
      creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
      game.Components.Add(creditsMenu);
      bgMenu = new BackgroundMenu(game, MenuDepths.Background);
      game.Components.Add(bgMenu);
      profileMenu.Animated = true;
      mainMenu.Enabled = false;
      creditsMenu.Enabled = false;
      settingsMenu.Enabled = false;
      mainMenu.Enabled = false;
      game.Components.Add(new MenuSoundManager(game));
      var menu = game.Services.GetService(typeof(IProfileMenu)) as IProfileMenu;
      menu.OnNewProfileClicked += () => { first_transition = true; game.ShowKeyboard(true); };
      menu.OnBackClicked += () => { first_transition = true; game.StartMenu(); };
      menu.OnReload += () => { game.ShowProfile(); };
    }

   
    /// <summary>
    /// Avviamo un livello; ci servono vari componenti:
    /// <list type="">
    /// <item>input manager per gestire gli eventi di input</item>
    /// <item>ui per disegnare l'interfaccia utente</item>
    /// <item>board renderer per disegnare le carte correnti</item>
    /// <item>board manager per gestire la logica delle carte, le
    /// AI, i giocatori umani, i turni, etc.</item>
    /// </list>
    /// 
    /// Il board_manager si occupa di lanciare due eventi che denotano la conclusione
    /// della partita; in relazione a tali eventi, lanciamo alternativamente il menu 
    /// oppure la schermata di recap del punteggio di fine partita.
    /// </summary>
    /// <param name="game"></param>
    /// <param name="index"></param>
    /// 
    static BoardRenderer board_renderer;
    static UIRenderer ui_renderer;
    static bool win = false;

    static public void StartLevel(this Game game, int index)
    {
      game.Cleanup();
      var input_manager = new InputManager(game);
      game.Components.Add(new GamePlaySoundManager(game));
      ui_renderer = new UIRenderer(game, MenuDepths.Overlay);
      board_renderer = new BoardRenderer(game);
      decorationPanel = new DecorationPanel(game, MenuDepths.Decoration);
      game.Components.Add(decorationPanel);
      var board_manager = new GamePlayLayer(game);
      game.Components.Add(input_manager);
      game.Components.Add(board_manager);
      game.Components.Add(board_renderer);
      game.Components.Add(ui_renderer);
      var level_manager = game.Services.GetService(typeof(ILevelManager<Score>)) as ILevelManager<Score>;
      decorationPanel.directionLeaf = 0;
      
      level_manager.OnLevelLost += () =>
      {
        // Save information like played games and time spent playing in the profile
        win = false;
        game.GetContext().currentProfile.gamesPlayed++;
        game.GetContext().currentProfile.timePlayed += ui_renderer.elapsedTime;
        
        game.SaveProfile(game.GetContext().currentProfile);
        board_renderer.Outro = true;
        game.LevelRecapScreen(index);
      };
      level_manager.OnLevelWon += score =>
        {
          // Save information like played games and time spent playing in the profile
          win = true;
          game.GetContext().currentProfile.gamesPlayed++;
          game.GetContext().currentProfile.gamesWon++;
          game.GetContext().currentProfile.timePlayed += ui_renderer.elapsedTime;

          game.SaveProfile(game.GetContext().currentProfile);
          board_renderer.Outro = true;
          game.LevelRecapScreen(index);
        };
      ui_renderer.OnHelpScreen += () => { first_transition = true; game.InGameHelpMenu(); };
      ui_renderer.OnMenuPause += () => { first_transition = true; game.InGameMenuPause(); };
      ui_renderer.OnDecoration += () => { game.InGameDecoration(); };
      ui_renderer.OnMatchExit += () => { first_transition = false; game.StartMenu(); };
      board_renderer.OnTutorial += () => { first_transition = true; game.StartTutorial(); };

#if WINDOWS_PHONE
      // Add phone event handlers
      var phoneApp = game.GetContext().PhoneApplicationService;
      phoneApp.Deactivated += (x, y) => {
        DefaultInfo info = game.LoadDefaults();
        var userId = game.GetContext().currentProfile.id;
        info.lastUser = userId;
        game.SaveDefaults(info);
        if (board_renderer.renderState != BoardRenderer.RenderState.Intro && board_renderer.renderState != BoardRenderer.RenderState.Outro)
        {
          SavedGame savedGame = new SavedGame { pack = board_manager.Pack, playerCards = board_manager.Players.ElementAt(0).Hand, timeElapsed = ui_renderer.elapsedTime, extractedCards = board_manager.ExtractedCards, difficulty = game.GetContext().currentConfig.difficulty };
          game.SaveGame(savedGame, userId);
        }
        game.SaveConfig(game.GetContext().currentConfig, userId);
        //game.SaveChart(game.GetContext().currentChart);
      };
      phoneApp.Closing += (x, y) => {
        DefaultInfo info = game.LoadDefaults();
        var userId = game.GetContext().currentProfile.id;
        info.lastUser = userId;
        game.SaveDefaults(info);
        if (board_renderer.renderState != BoardRenderer.RenderState.Intro && board_renderer.renderState != BoardRenderer.RenderState.Outro)
        {
          SavedGame savedGame = new SavedGame { pack = board_manager.Pack, playerCards = board_manager.Players.ElementAt(0).Hand, timeElapsed = ui_renderer.elapsedTime, extractedCards = board_manager.ExtractedCards, difficulty = game.GetContext().currentConfig.difficulty };
          game.SaveGame(savedGame, userId);
        }
        game.SaveConfig(game.GetContext().currentConfig, userId);
        //game.SaveChart(game.GetContext().currentChart);
      };
#endif
    }
    
    /// <summary>
    /// La schermata di recap di fine partita mostra i punteggi dei giocatori; tale
    /// schermata richiede l'input, che usa per lanciare l'evento Exit (se l'utente
    /// clicca sul bottone exit della schermata) oppure l'evento Continue (analogo
    /// ad Exit)
    /// </summary>
    /// <param name="game"></param>
    /// <param name="index"></param>
    /// <param name="score"></param>
    static private void LevelRecapScreen(this Game game, int index)
    {
      first_transition = true;
      //game.Cleanup();

      //board_renderer.Outro = true;
      board_renderer.Enabled = false;
      ui_renderer.Outro = true;

      game.Components.Add(new MenuSoundManager(game)); 

      board_renderer.OnExit += () =>
      {
        var level_recap = new LevelRecapScreen(game, MenuDepths.GameLayer, win);
        game.Components.Add(level_recap);

        decorationPanel.RoofEderEdirection = 1;
      

        level_recap.PlayAgain += () => { first_transition = false; game.GetContext().currentGame = null; game.StartLevel(1); };
        level_recap.BackToMain += () =>
        {
          // Delete saved game when the game is finished
          game.GetContext().currentGame = null;
          game.DeleteGame(game.GetContext().currentProfile.id);
          first_transition = false;
          game.StartMenu();
        };
      };      
    }

    /// <summary>
    /// Schermata di help in-game
    /// </summary>
    /// <param name="game"></param>
    static private void InGameHelpMenu(this Game game)
    {
      ui_renderer.Enabled = false; 
      board_renderer.Enabled = false;

      helpMenu = new HelpMenu(game, MenuDepths.ForeGround);
      //helpMenu.bounds.Inflate(-10, -10);
      game.Components.Add(helpMenu);

      helpMenu.Animated = true;
     
      var menu = game.Services.GetService(typeof(IHelpMenu)) as IHelpMenu;
      menu.OnHBackClicked += () =>
      {
        helpMenu.Dispose();
        board_renderer.Enabled = true;       
        ui_renderer.Enabled = true; 
      };
    }

    static private void InGameDecoration(this Game game)
    {
      first_transition = true;
      decorationPanel = new DecorationPanel(game, MenuDepths.Decoration);
      game.Components.Add(decorationPanel);
    }

    /// <summary>
    /// Mostra la schermata del menu in-game
    /// </summary>
    /// <param name="game"></param>
    static private void InGameMenuPause(this Game game)
    {
      ui_renderer.Enabled = false;
      board_renderer.Enabled = false;
     
      menuPause = new MenuPause(game, MenuDepths.ForeGround);
      menuPause.bounds.Inflate(-50, -50);
      menuPause.bounds.Location= new Point(900, 50);
      game.Components.Add(menuPause);

      menuPause.Animated = true;  

      var menu = game.Services.GetService(typeof(IMenuPause)) as IMenuPause;
      menuPause.OnMPBackClicked += () =>
      {
        menuPause.Dispose();
        board_renderer.Enabled = true;
        ui_renderer.Enabled = true;
      };

      menuPause.OnMPBackToMenu += () =>
      {
        var board_manager = game.GetBoardManager();
        SavedGame savedGame = new SavedGame { pack = board_manager.Pack, playerCards = board_manager.Players.ElementAt(0).Hand, timeElapsed = ui_renderer.elapsedTime, extractedCards = board_manager.ExtractedCards, difficulty = game.GetContext().currentConfig.difficulty };
        game.SaveGame(savedGame, game.GetContext().currentProfile.id);
        game.SaveConfig(game.GetContext().currentConfig, game.GetContext().currentProfile.id);
        first_transition = false;
        game.StartMenu();
      };

      menu.OnNewGame += () => { first_transition = false; game.GetContext().currentGame = null; game.StartLevel(1); };
    }

    /// <summary>
    /// Mostra la schermata del tutorial
    /// </summary>
    /// <param name="game"></param>
    static private void StartTutorial(this Game game)
    {
      ui_renderer.Enabled = false;
      ui_renderer.Visible = false;
      board_renderer.Enabled = false;
      decorationPanel.Enabled = false;

      // Load Tutorial component
      TutorialPanel tutorialPanel = new TutorialPanel(game, MenuDepths.ForeGround);
      //tutorialMenu.bounds.Inflate(-20, -20);
      game.Components.Add(tutorialPanel);

      tutorialPanel.OnOkClicked += () =>
      {
        board_renderer.Enabled = true;
        ui_renderer.Enabled = true;
        ui_renderer.Visible = true;
        decorationPanel.Enabled = true;
        tutorialPanel.Dispose();
      };
    }


    static public void ShowAds(this Game game)
    {
      game.Cleanup();
#if WINDOWS_PHONE
      List<Advertisement.GameTitle> gamesList = new List<Advertisement.GameTitle>();
      gamesList.Add(GameTitle.Dice3D);
      gamesList.Add(GameTitle.EbookReader);
      gamesList.Add(GameTitle.Enigma);
      gamesList.Add(GameTitle.Flashlight);
      gamesList.Add(GameTitle.Ideopuzzle);
      gamesList.Add(GameTitle.RisikoDice);
      gamesList.Add(GameTitle.SpaceBattle);
      gamesList.Add(GameTitle.Crossroads);
      var adsComponent = new Advertisement.AdsComponent(game, gamesList, 5.0f);
      game.Components.Add(adsComponent);
#endif
    }


    static public bool first_transition = true;

    /// <summary>
    /// Per evitare interferenze tra una fase di gioco e l'altra, cancelliamo tutti
    /// i game components attivi al termine di ciascuna fase di gioco.
    /// </summary>
    /// <param name="game"></param>
    static private void Cleanup(this Game game)
    {
      TransitionScreen transition = null;
      if (first_transition == false)
        transition = new TransitionScreen(game);
      for (int i = 0; i < game.Components.Count; i++)
      {
        if (game.Components[i] is TrialComponent) continue;
        ((GameComponent)game.Components[i]).Dispose();
        i--;
      }
      //game.Components.Clear();
      if (first_transition == false)
        game.Components.Add(transition);
      else
        first_transition = false;
    }
  }
}
